#include <Engine/StdAfx.h>
#include <Engine/Misc/StringUtils.h>

namespace fastbird
{
	//------------------------------------------------------------------------
	char* StripRight(char* s)
	{
		char* p = s + strlen(s);
		while ( p > s && isspace((unsigned char)(*--p)) )
			*p = '\0';
		return s;
	}

	//------------------------------------------------------------------------
	char* StripLeft(char* s)
	{
		while (*s && isspace((unsigned char)(*s)))
		    s++;
		return (char*)s;
	}

	//------------------------------------------------------------------------
	void StripExtension(char* s)
	{
		size_t len = strlen(s);
		while(--len)
		{
			if (s[len]=='.')
			{
				s[len]=0;
				break;
			}
		}
	}

	//------------------------------------------------------------------------
	const char* StripPath(const char* s)
	{
		size_t len = strlen(s);
		while(--len)
		{
			if (s[len]=='/')
			{
				return &s[len+1];
			}
		}
		return s;
	}

	//------------------------------------------------------------------------
	const char* GetExtension(const char* s)
	{
		size_t len = strlen(s);
		while(--len)
		{
			if (s[len]=='.')
			{
				return &s[len+1];
			}
		}

		return 0;
	}

	//------------------------------------------------------------------------
	bool CheckExtension(const char* filename, const char* extension)
	{
		const char* szExtension = GetExtension(filename);
		if (szExtension==0)
			return false;

		return stricmp(szExtension, extension)==0;
	}

	//------------------------------------------------------------------------
	void StepToDigit(char** ppch)
	{
		if (*ppch==0)
			return;

		int len = strlen(*ppch);
		for (int i=0; i<len; i++)
		{
			if (isdigit(**ppch))
				return;
			*ppch += 1;
		}

		*ppch = 0;
	}

	//------------------------------------------------------------------------
	void UnifyFilepath(char* szPath)
	{
		int len = strlen(szPath);
		for (int i=0; i<len; i++)
		{
			if (szPath[i]=='\\')
				szPath[i]='/';
		}
	}
}